package com.hope.onlinestudy.activity.info

import android.text.TextUtils
import com.google.gson.Gson
import com.hope.onlinestudy.base.BaseDataModel
import com.hope.onlinestudy.mvp.BasePresenterImpl
import com.hope.onlinestudy.net.HttpNetUtils
import com.hope.onlinestudy.net.NetworkScheduler
import com.hope.onlinestudy.net.ProgressSubscriber
import com.hope.onlinestudy.utils.ApiUtils

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class InfoPresenter :
    BasePresenterImpl<InfoContract.View>(),
    InfoContract.Presenter {
    override fun updateInfo(
        accout: String?,
        userName: String?,
        tel: String?,
        email: String?,
        sex: Int?
    ) {
        if (TextUtils.isEmpty(accout)) {
            mView?.showMsg("帐号不能为空")
            return
        }
        if (TextUtils.isEmpty(userName)) {
            mView?.showMsg("姓名不能为空")
            return
        }
        if (TextUtils.isEmpty(tel)) {
            mView?.showMsg("电话不能为空")
            return
        }
        if (TextUtils.isEmpty(email)) {
            mView?.showMsg("邮箱不能为空")
            return
        }

        val msg = Gson().toJson(
            hashMapOf<String, Any>(
                "userId" to ApiUtils.loginModel?.userId!!,
                "username" to accout!!,
                "truename" to userName!!,
                "usersex" to sex!!,
                "mobilephone" to tel!!,
                "useremail" to email!!
            )
        )
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.updateInfo}"
        HttpNetUtils.getInstance().getManager()?.updateInfo(url,ApiUtils.loginModel?.jwtToken!!, msg)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<BaseDataModel<String>>(mView) {
                override fun onSuccess(data: BaseDataModel<String>?, code: Int?) {
                    mView?.showMsg(data?.msg!!)
                }
            })
    }
}